package com.gpzx.zx.module.home.frame.view

import android.content.Context
import androidx.recyclerview.widget.LinearLayoutManager
import com.wkq.base.frame.mosby.delegate.MvpView
import com.gpzx.zx.module.home.frame.model.GpHomeOrgInfo
import com.gpzx.zx.module.home.frame.model.OrgInfo
import com.gpzx.zx.module.home.ui.activity.OrgInfoActivity
import com.gpzx.zx.module.home.ui.activity.OrgTypeListActivity
import com.gpzx.zx.module.home.ui.adapter.GpOrgAdapter
import com.gpzx.zx.module.home.ui.fragment.ExamAnnouncementFragment
import java.util.ArrayList

/**
 *
 * 作者:吴奎庆
 *
 * 时间:2020/6/15
 *
 * 用途:
 */


class ExamAnnouncementView : MvpView {

    var mFragment: ExamAnnouncementFragment
    var mAdapter: GpOrgAdapter? = null

    constructor(mFragment: ExamAnnouncementFragment) {
        this.mFragment = mFragment
    }

    fun initView() {
        initToolBar("机构")
        mFragment.binding.rvContent.layoutManager = LinearLayoutManager(mFragment.activity)
        mAdapter = GpOrgAdapter(mFragment.activity as Context)
        mFragment.binding.rvContent.adapter = mAdapter

        mAdapter?.setOnViewClickListener(object : GpOrgAdapter.OnAdapterViewClickListener<GpHomeOrgInfo> {
            override fun onViewClick(v: Int, program: GpHomeOrgInfo?) {
                if (v == 1) {
                    program?.title?.let { OrgTypeListActivity.newInstance(mFragment.activity as Context, it) }
                } else {
                    OrgInfoActivity().newInstance(mFragment.activity as Context, program?.newInfo?.id + "")

                }
            }


        })


    }

    private fun initToolBar(title: String?) {
        mFragment.binding.tvTitle.setText(title)
    }

    fun showView(list: List<OrgInfo>) {
        var content: MutableList<GpHomeOrgInfo> = ArrayList()

        var newTypes: MutableList<String> = ArrayList()


        list.forEach {
            if (!newTypes.contains(it.o_type)) {
                newTypes.add(it.o_type!!)
                var homegroupInfo = GpHomeOrgInfo()
                homegroupInfo.title = it.o_type
                homegroupInfo.type = 1
                content.add(homegroupInfo)
                var orgName: String? = ""
                var cont = 0;
                list.forEach {
                    if (homegroupInfo.title.equals(it.o_type) && !orgName.equals(it.o_name)) {
                        var contentInfo = GpHomeOrgInfo()
                        orgName = it.o_name
                        contentInfo.type = 2
                        contentInfo.title = it.o_type
                        contentInfo.newInfo = it
                        if (cont < 4) {
                            cont += 1
                            content.add(contentInfo)
                        }

                    }
                }

            }
        }

        mAdapter?.addItems(content)

    }

}